package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * 云南佬社区
 */
public class YnlaoBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url){
		boolean bRet = false;
		String[] regex = {
				"http://club.ynlao.net/[a-z]*/yn[0-9]*/1",
				"http://club.ynlao.net/forum.php\\?extra=.*&mod=viewthread&tid=[0-9]*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);
		title =doc.select("h1.ts").text();
		content = HtmlCleaner.getContentHtml(url, doc.select("div.pct").first());
		if(doc.select("div.pti").size()>0){
			String tempdate=doc.select("div.pti").get(0).select("em").html();
			date = DateUtils.matchDate(tempdate);
		}
		if(doc.select("div.authi a.xw1").size()>0){
			author = doc.select("div.authi a.xw1").get(0).text();
		}

		String text = doc.select("div.authi").text();
		text = StringUtils.substringAfter(text, "|");
		clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(text, "|"));
		commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "|"));

		if(StringUtils.isBlank(title)){
		}
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setAuthor(author);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String htmltxt = responseToURL.getHtml();
					Document doc = Jsoup.parse(htmltxt);
					Integer clickNum=0;
					Integer commentNum=0;
					String text = doc.select("div.authi").text();
					text = StringUtils.substringAfter(text, "|");
					clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(text, "|"));
					commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "|"));
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					return meta;
				}
			}
		}
		return null;
	}
    
	
}
